package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.vo.resp.DatasourceDetailRespVo;
import com.task.core.vo.resp.ProxyDetailRespVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.ProxyRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.ProxyService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:32
 */
@Api("")
@RestController
@RequestMapping("/proxy")
public class ProxyController extends BaseController {


  @Resource
  private ProxyService proxyService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('proxy:list')")
  @GetMapping("/getProxyByPage")
  public TableDataInfo getProxyByPage(ProxyRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(proxyService.getProxyByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('proxy:option')")
  @GetMapping("/getProxyForOption")
  public TableDataInfo getProxyForOption(ProxyRequestDTO request) {
    return getDataTable(proxyService.getProxyForOption(request));
  }

  @Log(title = "代理管理", businessType = BusinessType.INSERT)
  @PostMapping("/addProxy")
  public AjaxResult addProxy(@RequestBody ProxyRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    proxyService.addProxy(request);
    return success();
  }

  @Log(title = "代理管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyProxy")
  public AjaxResult modifyProxy(@RequestBody ProxyRequestDTO request) {
    checkService.check("data_proxy", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    proxyService.modifyProxy(request);
    return success();
  }

  @Log(title = "代理管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteProxy/{proxyCode}")
  public AjaxResult deleteProxy(@PathVariable String proxyCode) {
    checkService.check("data_proxy", proxyCode);
    proxyService.deleteProxy(proxyCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @GetMapping ("/getProxyDetail/{proxyCode}")
  @PreAuthorize("@ss.hasPermi('system:proxy:edit')")
  public AjaxResult getProxyDetailDetail(@PathVariable String proxyCode) {
    checkService.check("data_proxy", proxyCode);
    ProxyDetailRespVo proxyDetailRespVo = proxyService.getProxyDetailDetail(proxyCode);
    return success(proxyDetailRespVo);
  }

}
